package com.bdqn.design.singleton;

/**
 * Singleton03 单例模式（三：双检锁）
 *
 * @author LILIBO
 * @since 2023-10-25
 */
public class Singleton03 {

    // 该类的唯一对象
    private static Singleton03 instance = null;

    // ① 私有化构造方法
    private Singleton03() {
        String str = "100G";
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    // ② 提供一个静态的公有方法，返回创建的或者获取本身的静态私有对象
    public static Singleton03 getInstance() {
        if (instance == null) {
            synchronized (Singleton03.class) {
                instance = new Singleton03();
            }
        }
        return instance;
    }

    // 模拟序号
    private int index = 0;

    /**
     * 获取下一个序号
     *
     * @return
     */
    public synchronized int next() {
        return ++index;
    }

}
